package com.ssi.myshop.Presenter.impl;

import android.util.Log;

import com.ssi.myshop.Presenter.IRegisterPresenter;
import com.ssi.myshop.base.BaseData;
import com.ssi.myshop.bean.RegisterBean;
import com.ssi.myshop.contants.Api;
import com.ssi.myshop.base.BaseObserver;
import com.ssi.myshop.utils.RetrofitManager;
import com.ssi.myshop.utils.RxUtil;
import com.ssi.myshop.view.IRegisterCallBack;

import java.util.HashMap;

import io.reactivex.Observable;
import retrofit2.Retrofit;

public class IRegisterPresentImpl implements IRegisterPresenter {
    private static final String TAG = "IRegisterPresentImpl";

    private IRegisterCallBack mCallBack;
    private Retrofit retrofit;


    @Override
    public void doRegister(String name, String pwd) {
//        if (mCallBack != null) {
//            mCallBack.onLoading();
//        }

        retrofit = RetrofitManager.getInstance().getRetrofit();
        Api api = retrofit.create(Api.class);
        HashMap<String, Object> params = new HashMap<>();
        params.put("username", name);
        params.put("pwd", pwd);
        Observable<BaseData<RegisterBean>> observable = api.register(params);
        observable.compose(RxUtil.schedulerTransformer())
                .subscribeWith(new BaseObserver<RegisterBean>(mCallBack) {

                    @Override
                    protected void onSuccess(RegisterBean data) {
                        if (data != null) {
                        }
                        mCallBack.onRegisterSuccess();
                    }
                });
//        observable.subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new Observer<BaseData<RegisterBean>>() {
//                    @Override
//                    public void onSubscribe(Disposable d) {
//                        Log.d(TAG, "开始采用subscribe连接");
//                    }
//
//                    @Override
//                    public void onNext(BaseData<RegisterBean> registerBeanBaseData) {
//                        Log.d(TAG, "onNext: "+registerBeanBaseData.getMsg());
//                    }
//
//                    @Override
//                    public void onError(Throwable e) {
//                        Log.d(TAG, "onError: "+e.getMessage());
//                        Log.d(TAG, "onError: "+e.toString());
//                    }
//
//                    @Override
//                    public void onComplete() {
//                        Log.d(TAG, "请求成功");
//                    }
//                });

    }

    @Override
    public void registerViewCallBack(IRegisterCallBack callBack) {
        this.mCallBack = callBack;
    }

    @Override
    public void unregisterViewCallBack(IRegisterCallBack callBack) {
        this.mCallBack = null;
    }
}
